package com.zhihesj.anan.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.zhihesj.anan.system.entity.UserAuths;
import com.zhihesj.anan.utils.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.time.Clock;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

/**
 * @author anan.
 * @version 1.0
 * @create 2020-06-03 11-21
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    /**
     * 新增自动填充方法
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill...");
        String user ="admin";
        try {
            user = SecurityUtil.getUser();
        } catch (Exception e) {
            System.out.println("跳出这个设置");
        }
        this.setFieldValByName("createTime", LocalDateTime.now(Clock.system(ZoneId.of("Asia/Shanghai"))), metaObject);
        this.setFieldValByName("createBy", user, metaObject);
        this.setFieldValByName("updateTime", LocalDateTime.now(Clock.system(ZoneId.of("Asia/Shanghai"))), metaObject);
        this.setFieldValByName("updateBy", user, metaObject);
    }

    /**
     * 修改自动填充方法
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill...");
        String user =null;
        try {
            user = SecurityUtil.getUser();
        } catch (Exception e) {
            System.out.println("跳出这个设置");
        }
        this.setFieldValByName("updateTime", LocalDateTime.now(Clock.system(ZoneId.of("Asia/Shanghai"))), metaObject);
        this.setFieldValByName("updateBy", user, metaObject);
    }
}
